package com.ilibrary.service.impl;

import java.util.HashMap;
import java.util.List;

import com.ilibrary.base.MyBaseVO;
import com.ilibrary.constants.MyConstants;
import com.ilibrary.constants.MyMsgConstants;
import com.ilibrary.db.dao.hibernate.BookDao;
import com.ilibrary.db.dao.hibernate.CategoryDao;
import com.ilibrary.db.dao.hibernate.CollectDao;
import com.ilibrary.db.dao.hibernate.UserInfoDao;
import com.ilibrary.db.entity.Book;
import com.ilibrary.db.entity.Category;
import com.ilibrary.db.entity.Collect;
import com.ilibrary.db.entity.UserInfo;
import com.ilibrary.service.CollectService;
import com.ilibrary.utils.MyColumnIndicator;
import com.ilibrary.web.dto.ResultDto;
import com.ilibrary.web.vo.CollectVO;

public class CollectServiceImpl implements CollectService {
	private CollectDao collectDao;
	private CategoryDao categoryDao;
	private BookDao bookDao;
	private UserInfoDao userInfoDao;

	public CollectDao getCollectDao() {
		return collectDao;
	}
	public void setCollectDao(CollectDao collectDao) {
		this.collectDao = collectDao;
	}
	public CategoryDao getCategoryDao() {
		return categoryDao;
	}
	public void setCategoryDao(CategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}
	public BookDao getBookDao() {
		return bookDao;
	}
	public void setBookDao(BookDao bookDao) {
		this.bookDao = bookDao;
	}
	public UserInfoDao getUserInfoDao() {
		return userInfoDao;
	}
	public void setUserInfoDao(UserInfoDao userInfoDao) {
		this.userInfoDao = userInfoDao;
	}
	
	@Override
	public ResultDto<MyBaseVO> add(CollectVO collectVO) {
		ResultDto<MyBaseVO> resultDto = new ResultDto<MyBaseVO>();
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		Book book = bookDao.findById(collectVO.getBookId());
		UserInfo userInfo = userInfoDao.findById(collectVO.getUserId());
		
		map.put("book", book);
		map.put("userInfo", userInfo);
		List<Collect> collectList = collectDao.findByExample(map);
		if(collectList.size() != 0) {
			resultDto.setSuccess(false);
			resultDto.setMsg(MyMsgConstants.COLLECT_IS_EXIST);
			return resultDto;
		}
		
		Collect collect = new Collect();
		collect.setCollectId(collectVO.getCollectId());
		collect.setBook(book);
		collect.setUserInfo(userInfo);
		collectDao.add(collect);
		
		resultDto.setSuccess(true);
		return resultDto;
	}
	
	@Override
	public ResultDto<CollectVO> findByUserId(String userId) {
		ResultDto<CollectVO> resultDto = new ResultDto<CollectVO>();
		UserInfo userInfo = userInfoDao.findById(userId);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("userInfo", userInfo);
		List<Collect> collectList = collectDao.findByExample(map);
		CollectVO collectVO = null;
		Book book = null;
		Category category = null;
		for (Collect collect : collectList) {
			collectVO = new CollectVO();
			collectVO.setCollectId(collect.getCollectId());
			collectVO.setBookId(collect.getBook().getBookId());
			collectVO.setUserId(collect.getUserInfo().getUserId());
			
			book = bookDao.findById(collect.getBook().getBookId());
			collectVO.setBookName(book.getBookName());
			collectVO.setBookAuthor(book.getBookAuthor());
			collectVO.setBookStatus(book.getBookStatus());
			if(book.getBookStatus() == MyColumnIndicator.BOOK_STATUS_IN_LIB) {
				collectVO.setBookStatusString(MyColumnIndicator.BOOK_STATUS_IN_LIB_STRING);
			}
			if(book.getBookStatus() == MyColumnIndicator.BOOK_STATUS_BE_LOAN) {
				collectVO.setBookStatusString(MyColumnIndicator.BOOK_STATUS_BE_LOAN_STRING);
			}
			if(book.getBookStatus() == MyColumnIndicator.BOOK_STATUS_BE_LOSE) {
				collectVO.setBookStatusString(MyColumnIndicator.BOOK_STATUS_BE_LOSE_STRING);
			}
			
			category = categoryDao.findById(book.getCategory().getCategoryId());
			collectVO.setCategoryId(category.getCategoryId());
			collectVO.setCategoryName(category.getCategoryName());
			
			resultDto.addToResultList(collectVO);
		}
		return resultDto;
	}

	@Override
	public ResultDto<CollectVO> findById(int collectId) {
		ResultDto<CollectVO> resultDto = new ResultDto<CollectVO>();
		Collect collect = collectDao.findById(collectId);
		if(collect == null) {
			resultDto.setSuccess(false);
			resultDto.setMsg(MyMsgConstants.COLLECT_IS_NOT_EXIST);
		} else {
			CollectVO collectVO = new CollectVO();
			collectVO.setCollectId(collect.getCollectId());
			collectVO.setBookId(collect.getBook().getBookId());
			collectVO.setUserId(collect.getUserInfo().getUserId());
			
			Book book = bookDao.findById(collect.getBook().getBookId());
			collectVO.setBookName(book.getBookName());
			collectVO.setBookAuthor(book.getBookAuthor());
			collectVO.setBookStatus(book.getBookStatus());
			if(book.getBookStatus() == MyColumnIndicator.BOOK_STATUS_IN_LIB) {
				collectVO.setBookStatusString(MyColumnIndicator.BOOK_STATUS_IN_LIB_STRING);
			}
			if(book.getBookStatus() == MyColumnIndicator.BOOK_STATUS_BE_LOAN) {
				collectVO.setBookStatusString(MyColumnIndicator.BOOK_STATUS_BE_LOAN_STRING);
			}
			if(book.getBookStatus() == MyColumnIndicator.BOOK_STATUS_BE_LOSE) {
				collectVO.setBookStatusString(MyColumnIndicator.BOOK_STATUS_BE_LOSE_STRING);
			}
			
			Category category = categoryDao.findById(book.getCategory().getCategoryId());
			collectVO.setCategoryId(category.getCategoryId());
			collectVO.setCategoryName(category.getCategoryName());
			
			resultDto.addToResultList(collectVO);
			resultDto.setSuccess(true);
		}
		return resultDto;
	}
	
	@Override
	public ResultDto<MyBaseVO> deleteById(int collectId) {
		ResultDto<MyBaseVO> resultDto = new ResultDto<MyBaseVO>();
		collectDao.delete(collectId);
		resultDto.setSuccess(true);
		return resultDto;
	}
	
}
